﻿// ./about/About.cpp : 实现文件
//
#include "stdafx.h"
#include "ImgUi.h"
#include <ossDir.h>
#include <ossDll.h>
#include <ossString.h>
#include <ossList.h>
#include "labelDeviceBase.hpp"

#define SHOW_IMG_MSG	(LABEL_UI_MSG_MAX+1)
#define REPAINT_UI_MSG	(LABEL_UI_MSG_MAX + 2)
IMPLEMENT_DYNAMIC(ImgUi, LabelUiBase)

ImgUi::ImgUi(LabelUiBase* pParentDdfwUi, OssJson& createInfo)
    :LabelUiBase(pParentDdfwUi, createInfo)
	, m_pbyBitmatData(NULL)
{
	memset(&m_bi, 0, sizeof(m_bi));
}

ImgUi::~ImgUi()
{
	if (m_hBmp)
	{
		DeleteObject(m_hBmp);
		m_hBmp = nullptr;
	}
}

BEGIN_MESSAGE_MAP(ImgUi, LabelUiBase)
    ON_WM_CREATE()
	ON_WM_PAINT()
	ON_MESSAGE(SHOW_IMG_MSG, &ImgUi::OnShowImg)
	ON_MESSAGE(REPAINT_UI_MSG,&ImgUi::OnRepaint)
END_MESSAGE_MAP()



// About 消息处理程序
int ImgUi::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (LabelUiBase::OnCreate(lpCreateStruct) == -1)
        return -1;

	OssJson request;
	request["msgId"] = "decodeImg";
	request["msg"]["src"] = m_createInfo["src"];
	SendRequest(request, SHOW_IMG_MSG);

    return 0;
}

BOOL ImgUi::CreateBitmap(int iWidth, int iHeight, int iPlanes, int iBpp)
{
	BITMAPINFOHEADER& bmih = m_bi.bmiHeader;

	if (iWidth == bmih.biWidth && iHeight == bmih.biHeight && iPlanes == bmih.biPlanes
		&& iBpp == bmih.biBitCount)
	{
		return TRUE;
	}

	if (m_hBmp)
	{
		DeleteObject(m_hBmp);
		m_hBmp = nullptr;
	}

	memset(&bmih, 0, sizeof(BITMAPINFOHEADER));
	bmih.biSize = sizeof(BITMAPINFOHEADER);
	bmih.biWidth = iWidth;
	bmih.biHeight = iHeight;
	bmih.biSizeImage = bmih.biWidth * bmih.biHeight;
	bmih.biCompression = BI_RGB;
	bmih.biBitCount = iBpp;
	bmih.biPlanes = iPlanes;

	m_iBitmatDataMax = iWidth * iHeight * iBpp/8;

	m_hBmp = CreateDIBSection(NULL, &m_bi, DIB_RGB_COLORS, (void**)&m_pbyBitmatData, NULL, 0);
	if (m_hBmp)
	{
		return TRUE;
	}

	return FALSE;
}


void ImgUi::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	CRect rect;
	GetClientRect(&rect);

	if (m_hBmp)
	{
		BITMAP              bitmap;
		GetObject(m_hBmp, sizeof(BITMAP), &bitmap);

		CDC MemDC;
		MemDC.CreateCompatibleDC(&dc);
		MemDC.SelectObject(m_hBmp);

		dc.StretchBlt(0, 0, rect.Width(), rect.Height(), &MemDC, 0, 0, m_bi.bmiHeader.biWidth, m_bi.bmiHeader.biHeight, SRCCOPY);
	}
}

LRESULT ImgUi::OnShowImg(WPARAM wparam, LPARAM lparam)
{
	OssJson& imgInfo = *(OssJson*)wparam;
	OssJson& msg  = imgInfo["msg"];
	OssJson& format = msg["format"];
	OssJson& width = msg["width"];
	OssJson& height = msg["height"];
	OssJson& data = msg["data"];
	OssMutex* mutexPtr = (OssMutex*)msg["mutex"].asPointer();

	if (format.ValueType() != JSON_STRING_VAL || width.ValueType() != JSON_INT_VAL ||
		height.ValueType() != JSON_INT_VAL || data.ValueType() != JSON_POINTER_VAL )
	{
		WriteLog(LOG_PRIORITY_ERROR,"onshow img param error\n");
		return NULL;
	}

	//判断是否为超时反馈
	if (wparam == NULL)
	{
		WriteLog(LOG_PRIORITY_ERROR, "获取按钮信息超时\n");
		return NULL;
	}

	if (format != "RGB8")
	{
		WriteLog(LOG_PRIORITY_ERROR, "not support nonrgb8 format\n");
		return NULL;
	}

	if (!CreateBitmap(width.asInt(), height.asInt(), 1, 24))
	{
		WriteLog(LOG_PRIORITY_ERROR, "create bit map failed:width:%d,heigt:%d\n", width.asInt(), height.asInt());
		return NULL;
	}
	
	//这边不上锁，由外部上锁
	//if (mutexPtr)
	//{
	//	mutexPtr->Lock();
	//}
	BYTE* pcData = (BYTE*)data.asPointer();
	if (pcData)
	{
		memcpy(m_pbyBitmatData, pcData, width.asInt() * height.asInt() * 3);
	}

	if (mutexPtr)
	{
		mutexPtr->Unlock();
	}
	
	//if (OssGetTickCount() - m_dwLastUpdate > 30)
	{
		this->PostMessage(REPAINT_UI_MSG, NULL, NULL);
		m_dwLastUpdate = OssGetTickCount();
	}
	return NULL;
}

/**
	 *      更新UI
	 */
void ImgUi::UpdateUi(OssJson& updateUiInfo)
{
	OssJson& imgInfo = updateUiInfo;
	OssJson& msg = imgInfo["msg"];
	OssJson& format = msg["format"];
	OssJson& width = msg["width"];
	OssJson& height = msg["height"];
	OssJson& data = msg["data"];
	OssMutex* mutexPtr = (OssMutex*)msg["mutex"].asPointer();


	if (format.ValueType() != JSON_STRING_VAL || width.ValueType() != JSON_INT_VAL ||
		height.ValueType() != JSON_INT_VAL || data.ValueType() != JSON_POINTER_VAL)
	{
		WriteLog(LOG_PRIORITY_ERROR, "onshow img param error\n");
		return ;
	}

	if (format != "RGB8")
	{
		WriteLog(LOG_PRIORITY_ERROR, "not support nonrgb8 format\n");
		return ;
	}

	if (!CreateBitmap(width.asInt(), height.asInt(), 1, 24))
	{
		WriteLog(LOG_PRIORITY_ERROR, "create bit map failed:width:%d,heigt:%d\n", width.asInt(), height.asInt());
		return ;
	}

	//这边不上锁，由外部上锁
	//if (mutexPtr)
	//{
	//	mutexPtr->Lock();
	//}
	BYTE* pcData = (BYTE*)data.asPointer();
	if (pcData)
	{
		memcpy(m_pbyBitmatData, pcData, width.asInt() * height.asInt() * 3);
	}

	if (mutexPtr)
	{
		mutexPtr->Unlock();
	}
	if (OssGetTickCount() - m_dwLastUpdate > 30)
	{
		this->PostMessage(REPAINT_UI_MSG, NULL, NULL);
		m_dwLastUpdate = OssGetTickCount();
	}
}


LRESULT ImgUi::OnRepaint(WPARAM wparam, LPARAM lparam)
{
	this->Invalidate();
	this->UpdateWindow();

	return NULL;
}


static OssVersion g_version={
	"V3.0.0.0",
	"ImgUi",
	"吴建凰",
	__DATE__  __TIME__,
	"创建img驱动界面 吴建凰 2022年6月15日\n"   //不能加逗号，多次信息一次增加
	"\n\n"
};

LABEL_UI_ENTRY(ImgUi)

